package com.eascs.crm.nsEditDetail.service;

import java.util.Date;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.dao.NsEditDetailDao;
import com.eascs.crm.nsEditDetail.entity.CarInfoEntity;
import com.eascs.crm.nsEditDetail.entity.CarInfoLogEntity;
import com.eascs.crm.nsEditDetail.entity.OtherInfoEntity;
import com.eascs.crm.nsEditDetail.entity.OtherInfoLogEntity;
import com.eascs.crm.nsEditDetail.form.CarInfoForm;
import com.eascs.crm.nsEditDetail.form.OtherInfoForm;
import com.eascs.web.sso.UserHolder;

@Service
public class OtherInfoService {

	@Inject
	NsEditDetailDao nsEditDetailDao;

	public PageResult<Object[]> findCarListInfo(String loanapplyId,Integer showPage) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 10, showPage);
		StringBuilder sql = new StringBuilder();
		sql.append("select h.id,h.other_type,h.other_price,log.other_price  other_price_review from other_info h  left join other_info_log log on h.id =log.other_info_id  and log.op_date");
	    sql.append( "	    in (select max(ll.op_date) from other_info_log ll where ll.other_info_id=log.other_info_id) where 1=1 and h.is_del = 'N' ");
		if (StringUtil.isNotBlank(loanapplyId)) {
			sql.append(" and h.loanapply_no = ?");
			sqlPageQuery.addParam(loanapplyId);
		}
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> CarPage = nsEditDetailDao.findDataWithPage(sqlPageQuery);
		return CarPage;
	}
	
	public OtherInfoEntity findOtherInfoEntity(String otherId) {
		return nsEditDetailDao.getById(OtherInfoEntity.class, otherId);
	}
	
	public OtherInfoEntity findOtherInfoByLoanapplyId(String loanapplyId) {
		return nsEditDetailDao.findEntity("select * from other_info where loanapply_no = ?",OtherInfoEntity.class, loanapplyId);
	}

	public OtherInfoLogEntity findOtherLogEntity(String otherId) {
		return nsEditDetailDao.findEntity(" select * from other_info_log l where l.op_date in (select MAX(t.op_date) from other_info_log t where t.other_info_id =l.other_info_id   group by t.other_info_id ) and l.other_info_id = ? ",
				OtherInfoLogEntity.class, otherId);
	}
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public int  otherDel(String otherId){
		return nsEditDetailDao.execute("update  other_info set is_del ='Y'  where id = ?", otherId);
	}
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public int  otherSave(OtherInfoEntity other,OtherInfoForm form) {
		int i=0;
		setOtherPorperty(other, form);
		i=nsEditDetailDao.insert(other);
		i=saveLog(other);
		return  i;
	}
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public int otherUpdate(OtherInfoEntity other,OtherInfoForm form) {
		int i=0;
		setOtherPorperty(other, form);
		i=nsEditDetailDao.updateById(other);
		i=saveLog(other);
		return  i;
	}

	private int saveLog(OtherInfoEntity other) {
		OtherInfoLogEntity log= new OtherInfoLogEntity();
		log.setOtherInfoId(other.getId());
		log.setOpPsn(UserHolder.getUser().getCname());
		log.setOpNo(UserHolder.getUser().getCode());
		log.setOpDate(new Date());
		log.setOtherPrice(other.getOtherPrice());
		return nsEditDetailDao.insert(log);
	}

	private void setOtherPorperty(OtherInfoEntity other, OtherInfoForm form) {
		other.setOtherPrice(form.getOtherPrice());
		other.setOtherType(form.getOtherType());
		other.setIsDel("N");
		other.setLoanapplyNo(StringUtil.isBlank(form.getLoanapplyId())?other.getLoanapplyNo():form.getLoanapplyId());
		other.setId(form.getOtherId());
	}

	public <T> int insert(T entity) {
		return nsEditDetailDao.insert(entity);
	}

	public boolean checkOtherAssets(String loanapplyId){
		String sql="SELECT count(*) FROM other_info l INNER JOIN other_info_log log ON l.id = log.other_info_id WHERE log.op_date in (select MAX(t.op_date) from other_info_log t where t.other_info_id =log.other_info_id   group by t.other_info_id )  AND l.loanapply_no = ? and log.other_price is  null ";
		int i=nsEditDetailDao.findInteger(sql,loanapplyId);
		if(i>0){
			return true;
		}else{
			return false;
		}
	}

}
